package com.shenhua.kunpeng.utils;

import com.shenhua.kunpeng.dto.FillUserDTO;
import com.shenhua.kunpeng.entity.BaseEntity;

import java.time.LocalDateTime;
import java.util.Optional;

public class EntityUtils {

  public static <T extends BaseEntity> T fillId(T t) {
    t.setId(IdUtils.generatorId());
    return t;
  }

  public static <T extends BaseEntity> T fillDelete(T t) {
    FillUserDTO dto = FillUserDTOContextHolder.get();
    Optional.ofNullable(dto)
        .ifPresent(
            d -> {
              t.setModifiedBy(d.getUserId().toString());
              t.setModifiedName(d.getRealName());
              t.setDeleteBy(d.getUserId().toString());
              t.setDeleteName(d.getRealName());
            });

    t.setDeleteTime(LocalDateTime.now());
    t.setLastModifiedTime(LocalDateTime.now());
    t.setMark(0);
    return t;
  }
}
